﻿/****************************** Module Header ******************************\
 * Module Name:  LogHelper.cs
 * Module Version:  1.0.0 (2012-3-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * Write any words here.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;

namespace UCF.Core.Security.Log
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logfolder"></param>
        /// <param name="content"></param>
        /// <param name="logType"></param>
        public static void WriteLog(string logfolder,string content,LogType logType)
        {
            if (!Directory.Exists(logfolder)) Directory.CreateDirectory(logfolder);
            string sFileName=logfolder+"\\"+logType.ToString()+"_"+DateTime.Now.ToString("yyyy_MM_dd")+".txt";
            if (!File.Exists(sFileName))
            {
                File.WriteAllText(sFileName, FormatLogText(content));
                return;
            }
            File.WriteAllText(sFileName, File.ReadAllText(sFileName) + FormatLogText(content));
        }
        /// <summary>
        /// 读取日志
        /// </summary>
        /// <param name="logfolder"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ReadLog(string logfolder, string fileName)
        {
            string sFileName=logfolder+"\\"+fileName;
            return File.Exists(sFileName)?File.ReadAllText(sFileName):"Log Not Found!";
        }
        /// <summary>
        /// 跟踪页面错误日志
        /// </summary>
        /// <param name="logfolder"></param>
        public static void TracePageError(string logfolder)
        {
            Exception ex = System.Web.HttpContext.Current.Server.GetLastError();
            if (ex == null) return;
            WriteLog(logfolder, FormatLogText(ex.Message + Environment.NewLine + ex.Source),LogType.Error);
        }
        /// <summary>
        /// 跟踪攻击日志
        /// </summary>
        /// <param name="logfolder"></param>
        public static void TraceAttackError(string logfolder)
        {
            string strVal = System.Web.HttpContext.Current.Request.QueryString.ToString();
            System.Web.HttpContext.Current.Response.Write(strVal);
            if (strVal == null || strVal.Length==0) return;
            if (strVal.IndexOf("%27")==-1 && strVal.IndexOf('\'') == -1 && strVal.IndexOf("+and+") == -1) return;
            WriteLog(logfolder, strVal, LogType.Attack);
        }
        /// <summary>
        /// 格式化日志内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        /// <example>
        /// 2012-3-16 11:00:30
        /// 测试日志内容
        /// </example>
        private static string FormatLogText(string content)
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + content + Environment.NewLine;
        }
        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogType
        {
            Log=1,  // 普通日志
            Error=2,    // 系统错误
            Attack=3,   // 黑客攻击
            Spider=4    // 搜索引擎
        }
    }
}
